package cn.juque.datapipeline.utils;

import cn.hutool.core.text.CharSequenceUtil;
import cn.juque.common.constants.StringConstants;
import cn.juque.datapipeline.api.enums.DbEnum;

/**
 * @author nuoka
 * @version 1.0.0
 * <li></li>
 * @date 2022/7/21 10:55
 **/
public class PageUtil {

    private PageUtil() {
    }

    public static String buildPageInfo(String sql, DbEnum dbEnum, Integer pageNo, Integer pageSize) {
        int start = (pageNo - 1) * pageSize;
        String result = CharSequenceUtil.endWith(sql, StringConstants.SEMICOLON) ? CharSequenceUtil.subBefore(sql, StringConstants.SEMICOLON, true) : sql;
        switch (dbEnum) {
            case MYSQL:
                return result + " limit " + start + StringConstants.COMMA + pageSize;
            case ORACLE:
                start = start + 1;
                return "SELECT TMP_CACHE_2.* FROM (SELECT ROWNUM AS RN, TMP_CACHE_1.* FROM (" + result + ") TMP_CACHE_1 WHERE ROWNUM < " + (start + pageSize) + ") TMP_CACHE_2 WHERE TMP_CACHE_2.RN >=" + start;
            default:
                break;
        }
        return CharSequenceUtil.EMPTY;
    }
}
